package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateAddNewParam;
import cn.tedu.csmall.product.pojo.param.AttributeTemplateUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/attribute-template")
@Api(tags = "06. 属性模版管理模块")
@Slf4j
@Validated
public class AttributeTemplateController {

    @Autowired
    private IAttributeTemplateService attributeTemplateService;


    @PostMapping("/add-new")
    @ApiOperation("添加属性模版")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AttributeTemplateAddNewParam attributeTemplateAddNewParam) {
        attributeTemplateService.addNew(attributeTemplateAddNewParam);
        return JsonResult.ok();


    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除属性模版")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性模版ID", required = true, dataType = "long"),

    })
    public JsonResult delete(@Range(min = 1, message = "根据ID删除属性模版失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据id删除属性模版】的请求，参数：{}", id);
        attributeTemplateService.delete(id);
        log.debug("处理【根据id删除属性模版】的请求，完成！");
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改属性模版")
    @ApiOperationSupport(order = 300)
    public JsonResult updateInfoById(@Range(min = 1, message = "根据ID更新属性模版失败，请提交合法的ID值！")
                                     @RequestParam Long id,
                                     @Valid AttributeTemplateUpdateInfoParam attributeTemplateUpdateInfoParam) {
        log.debug("开始处理【修改属性模版】的请求，参数：{}", id, attributeTemplateUpdateInfoParam);
        attributeTemplateService.updateInfoById(id, attributeTemplateUpdateInfoParam);
        log.debug("处理【修改属性模版】的请求，完成！");
        return JsonResult.ok();
    }


    @GetMapping("/list")
    @ApiOperation("查询属性模板列表")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码",required = true,dataType = "Integer")
    })
    public JsonResult list(@Range(min = 1,message = "请提供正确的页码值") Integer pageNum,String queryType) {
        log.debug("开始处理【查询属性模板列表】业务！");
        PageData<AttributeTemplateListItemVO> list;
        if ("all".equals(queryType)) {
            list = attributeTemplateService.list(pageNum, Integer.MAX_VALUE);
        } else {
            list = attributeTemplateService.list(pageNum);
        }
        log.debug("处理【查询属性模板列表】完毕！");
        return JsonResult.ok(list);
    }

    @GetMapping("/getStandard")
    @ApiOperation("根据id查询属性模板")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long")
    })
    public JsonResult getStandard(Long id) {
        log.debug("开始处理【根据id查询属性模板】业务！");
        AttributeTemplateStandardVO standard = attributeTemplateService.getStandard(id);
        log.debug("处理【根据id查询属性模板】完成！");
        return JsonResult.ok(standard);
    }
}
